<template>
  <el-row :gutter="20">
    <!-- 左侧部门树 -->
    <el-col :span="4" :xs="24">
      <ContentWrap class="h-1/1">
        <DeptTree @node-click="handleDeptNodeClick" />
      </ContentWrap>
    </el-col>
    <el-col :span="20" :xs="24">
      <ContentWrap>
        <!-- 搜索工作栏 -->
        <el-form
          class="-mb-15px"
          :model="queryParams"
          ref="queryFormRef"
          :inline="true"
          label-width="68px"
        >

          <el-form-item label="实践点" prop="spotId">
            <el-input
              v-model="queryParams.spotId"
              placeholder="请输入实践点"
              clearable
              @keyup.enter="handleQuery"
              class="!w-240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          </el-form-item>
        </el-form>
      </ContentWrap>

      <!-- 列表 -->
      <ContentWrap>
        <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
          <el-table-column label="实践点名称" align="center" prop="spotName" width="250"/>
          <el-table-column label="归属地" align="center" prop="belongAddress"  width="200"/>
          <el-table-column label="评论人" align="center" prop="nickName" width="150"/>
          <el-table-column label="星星数" align="center" width="200">
            <template #default="scope">
              <el-rate v-model="scope.row.stars" />
            </template>
          </el-table-column>
          <el-table-column label="评论内容" align="center" prop="content" width="250"/>
          <el-table-column label="状态" align="center" width="100" fixed="right">
            <template #default="scope">
              {{formatStatus(scope.row.commentStatus) }}
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" fixed="right" width="150">
            <template #default="scope">
              <el-popover placement="top" :width="160" v-if="scope.row.commentStatus === 1">
                <p style="text-align: center;">是否通过</p>
                <div style="display: flex;flex-flow: row nowrap;justify-content: space-between;">
                  <el-button style="flex: 1" size="small" @click="handlePass(scope.row.id, 3)">退回</el-button>
                  <el-button style="flex: 1" size="small" type="primary" @click="handlePass(scope.row.id, 2)">通过</el-button>
                </div>
                <template #reference>
                  <el-button
                    link
                    type="primary"
                    v-hasPermi="['scenic:comment:update']"
                  >
                    审核
                  </el-button>
                </template>
              </el-popover>
              <el-button
                link
                type="danger"
                v-hasPermi="['scenic:comment:delete']"
                @click="handleDelete(scope.row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <Pagination
          :total="total"
          v-model:page="queryParams.pageNo"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </ContentWrap>
    </el-col>
  </el-row>


  <!-- 表单弹窗：添加/修改 -->
  <CommentForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { CommentApi, CommentVO } from '@/api/scenic/comment'
import CommentForm from './CommentForm.vue'
import DeptTree from './DeptTree.vue'

/** 实践点评论 列表 */
defineOptions({ name: 'Comment' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<CommentVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  memberId: undefined,
  content: undefined,
  isShow: undefined,
  spotId: undefined,
  spotDeptId: undefined,
  stars: undefined,
})
const queryFormRef = ref() // 搜索的表单

const formatStatus = computed(() => {
  return function(commentStatus){
    let commentStatusStr = '--'
    switch(commentStatus){
      case 1:
        commentStatusStr = '待审核'
        break;
      case 2:
        commentStatusStr = '通过'
        break;
      case 3:
        commentStatusStr = '退回'
        break;
      default:
        break;
    }
    return commentStatusStr;
  }
})

const handleDeptNodeClick = async (row) => {
  queryParams.spotDeptId = row.id
  await getList()
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CommentApi.getCommentPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await CommentApi.deleteComment(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})

const handlePass = async (id, commentStatus = 1) => {
  if(!id) return
  const data = await CommentApi.getComment(id)
  await CommentApi.updateComment({ ...data, commentStatus})

  getList()
}
</script>
